package comm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import objects.Robot;

/**
 * 
 * @author ruben.ortlam
 *
 */
public class Client {
	public String name = "";
	public Socket socket;
	public long lastPing;
	
	public ClientInputListener in;
	public DataOutputStream out;
	
	public Thread inThread;
	
	public Robot robot;
	public long lastRespawn;
	
	private boolean isIdentified;
	public String preferredName;
	public int preferredColor;
	
	/**
	 * Creates a Client with the given Socket.
	 *
	 * @param socket The Socket holding the connection to the client.
	 */
	public Client(Socket socket) {
		this.socket = socket;
		try {
			out = new DataOutputStream(socket.getOutputStream());
			out.flush();
			in = new ClientInputListener(new DataInputStream(socket.getInputStream()), this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		inThread = new Thread(in);
		inThread.start();
		
		lastPing = System.currentTimeMillis();
		lastRespawn = 0L;
	}
	/**
	 * Returns if the client has disconnected.
	 *
	 * @return true if the client is disconnected, false otherwise
	 */
	public boolean isDisconnected() {
//		System.out.println(lastPing + ":" + System.currentTimeMillis() + ":" + (System.currentTimeMillis() - lastPing));
		if(System.currentTimeMillis() - lastPing >= 3000) {
			return true;
		}
		return false;
	}
	
	public boolean isIdentified() {
		return isIdentified;
	}
	public void setIdentified(boolean isIdentified) {
		this.isIdentified = isIdentified;
	}
}
